/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"


static volatile unsigned char raw_data[3] = {0};



/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay (void)
{
	unsigned int i;
	
	for( i = 0 ; i < 200 ; i++ ){}
}


/**
 * @brief       SDMISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void SDMISR (void) interrupt ISR_SDM
{
	unsigned char int_stat = SDM->intstat;             
	
	SDM->intstat = 0x81;   // clear interrupt flag
	
	/* fetch calibration data */
	raw_data[0] = SDM->rawdata[0];	// L_byte
	raw_data[1] = SDM->rawdata[1];	// M_byte
	raw_data[2] = SDM->rawdata[2];  // H_byte
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       SDMConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void SDMConfig (void)
{
		// RTD
	
		//Set OSR
		OSR_32768();
	
		//Set INMUX
		Set_input_channel_Ch6();
	
		//Set Rmux
		Set_Vref_1p2V();
		
		//Set INBUF
		Enable_Input_buffer();
		
		//Set VRBUF
		Enable_Vref_buffer();
	
		//Set Vref Gain
		Set_Vref_Gain_x1_div4(); 		// 1/4Vref  -> 1.2/4 = 0.3V
	
		//Set ADCGN
		Set_ADC_Gain_x1();
		
		//Enable TMPSNS
		Enable_TMPSNS();
		
		//Enable ADC
		Enable_ADC();
		
		//	Enable Interrupt
		Enable_SDM_inerrupt();

}


/**
 * @brief       ANACConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void ANACConfig ()
{
	//	Enalbe BIAS LDOA VCM
	Enable_BIAS();
	Enable_LDOA();
	Enable_VCM();
	
	//	Enable DAC1P
	Enable_DAC1P();
	
	//	Enable PSW
	Enable_PSW();
	
	//	Enable DAC1
	Enable_IDAC1();
	
	//	Set DAC1 Current 
	Set_IDAC1_Current(Current_1200uA);
	
}

/**
 * @brief       IOConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void IOConfig()
{
	/* Set PC7 input as button */
	PORT_SetPCInput(BIT7);

	/* Set PD1. ,PD2.,PD3 output */
	PORT_SetPDPushPull(BIT1 | BIT2 | BIT3 );

	/* LED Ini */
	PD1 = 0; // LED_G1 On
	PD2 = 1 ; //LED_R Off
	PD3= 0;  // LED_G2  On
}


/**
 * @brief       CLKConfig function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CLKConfig()
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();
	
	/* enable SDM & ANA clock */
	En_SDM_clk();
	En_ANA_clk();
	
	/* sdm clock divider */
	Set_SDM_clk_250K();   
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main (void)
{
	CLKConfig();
	IOConfig();
	
	ANACConfig();
	SDMConfig();
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
	
	delay();
	
	while(1);
}

